// pages/module/growth/growth.js
var app = getApp();
import shareFun from '../../../utils/shareFun.js';
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import {
  recordGrowth,
  getGrownList,
  getSignStatus,
  getGrowth,
  level2_upgrade
} from '../../../utils/imapi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date_list:[],
    renwu_list:[
      {title:'绑定手机号',btn_name:'去绑定',text1:'+1积分',text2:'+0积分',text3:'今日0/1次'},
      {title:'绑定手机号',btn_name:'去绑定',text1:'+1积分',text2:'+0积分',text3:'今日0/1次'},
      {title:'绑定手机号',btn_name:'去绑定',text1:'+1积分',text2:'+0积分',text3:'今日0/1次'},
      {title:'绑定手机号',btn_name:'去绑定',text1:'+1积分',text2:'+0积分',text3:'今日0/1次'},
    ],
    height: (app.globalData.screenWidth/750 *676 - app.globalData.CustomBar) || 278,
    is_sign:false,
    levelIndex:0,
    allGrowNum:500,
    growNum:300,
    left_list:['95','360','625'],
    userInfo:{
      level_code:'1'
    },
    img_list:"img_list.img_list"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow:async function (options) {
    this.setData({
      img_list:img_list.img_list
    })
    
    if(app.globalData.uid == '0'){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    var curTime = new Date()
    var timestamp = Date.parse(curTime);
    var mouth = (curTime.getMonth()+1);
    var day = curTime.getDate();
    var today = (mouth > 9 ? mouth : ('0' + mouth)) + '-' + (day > 9 ? day : ('0' + day));
    console.log(today)
    var is_sign = false
    const {data} = await getSignStatus({duration:timestamp})
    data.forEach(item=>{
      item.day_time = item.day_time.substring(5,item.day_time.length)
      item.is_come = 0;
      if(today == item.day_time){
        if(item.check_in == 1){
          is_sign = true
        }
        item.is_come = 1
      }
      item.jifen = 0.02
    })
    const renwu_list = await getGrownList()
	console.log("操作到这里了吗？");
  console.log(renwu_list)
 

   let levelIndex = parseInt(app.globalData.userInfo.level_code) +1

   if(levelIndex>2){
      levelIndex=2
   }

    this.setData({
      is_sign,
      levelIndex:levelIndex,
      today,
      toView:'c'+today,
      date_list:data,
      renwu_list:renwu_list.data,
      width:540 * 80 / 100,
      userInfo:app.globalData.userInfo || this.data.userInfo
    })
    const growThData = await getGrowth();
    this.setData({
      today_value:growThData.data.today_value,
      total_value:growThData.data.total_value,
      cleft:growThData.data.total_value/3500 * 332 + 166,
    })
  },
  onPageScroll: function (e) {
    var that = this;
    this.scrollTop = e.scrollTop
    if(e.scrollTop>100 && this.data.cur_scroll_top==100) return;
    that.setData({
      cur_scroll_top:e.scrollTop>100?100:e.scrollTop
    })
 },
 async goSign(){
    const {data} = await recordGrowth({tasktype:1})
    this.data.date_list.forEach(item=>{
      if(this.data.today == item.day_time){
        item.check_in = 1
      }
    })
    this.setData({
      date_list:this.data.date_list,
      is_sign:true
    })
    const growThData = await getGrowth();
    this.setData({
      today_value:growThData.data.today_value,
      total_value:growThData.data.total_value
    })
 },
  SetShadow(e) {
    this.setData({
      shadow: e.detail.value
    })
  },
  focuson(){
	  var img='https://img.xingkonglian.net/img/common/qrcode_xpshfwh.jpg';
	  var cg=[];
	  cg.push(img);
	  wx.previewImage({
	    current: img, 
	    urls: cg
	   })
  },
  showVip(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      levelIndex:index
    })
  },
  goVisit(){
    wx.navigateTo({
      url: '/packageTab/pages/visit/visit',
    })
  

  },
  upgradeLevel2(){
   // 升级成为导师， 调用接口 导师检测

    if (app.globalData.userInfo.level_code==0){
       wx.showToast({
         title: '请先升级到推客',
       })
       return 
    }


    // 调用接口

    wx.showLoading({
      title: '升级检测中...',
    })
    
    level2_upgrade({to_level_code:'2'})

    wx.hideLoading({
      success: (res) => {},
    })

    getCurrentPages()[getCurrentPages().length - 1].onLoad()

  },
  
  goBind(){
    wx.navigateTo({
      url: '/pages/module/setting/setting',
    })
  },
  goGrowUp(){
    wx.navigateTo({
      url: './growthUp/growthUp',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function () {
    if(!app.is_auth()){
      wx.showToast({
        title: '您还未登陆，请登陆后再分享可以赚佣金哦',
      })
    }
    return shareFun.shareIndex(app.globalData.userInfo.uid,app.globalData.userInfo.wx_nickname);
  }
})